<?php
namespace App\Services\H5;

use App\Services\BaseService as Service;
use Illuminate\Support\Facades\DB;

class BaseService extends Service
{
    /**
     * 服务类，写复杂的逻辑
     */

    //通过手机号，获取UID
    public function uidByPhone($phone)
    {
        if (!$phone) return 0;
        $res = (array) $this->dbConn($this->dbUser)
            ->where('phone', $phone)
            ->first();
        return empty($res) ? 0 : $res['id'];
    }

    /**
     * 获取广告
     * @return array
     */
    public function getAdOne()
    {
        $adOne = (array) $this->dbConn($this->dbAdList)
            ->where('num', '>', 0)
            ->orderBy('id', 'asc')
            ->first();
        if (empty($adOne)) {
            return [
                'ad_id' => 0,
                'uid' => 0,
                'thumb' => getenv('DOMAIN').'/h5/imgs/ad-img-2.png',
                'link' => '',
            ];
        }
        //设置广告记录
        $usedData = [
            'ad_id' => $adOne['id'],
            'pub_uid' => $adOne['uid'],
            'addtime' => toDate(),
        ];
        $lastid = $this->dbConn($this->dbAdUsed)->insertGetId($usedData);
        return [
            'ad_id' => $adOne['id'],
            'use_id' => $lastid,
            'thumb' => env('DOMAIN').'/'.$adOne['thumb'],
            'link' => $adOne['link'],
        ];
    }

    //返回格式
    public function toback($code = 0, $msg = '', $data = [], $total = 0)
    {
        $msg = $msg ? $msg : 'success';
        return array('code' => $code, 'msg' => $msg, 'data' => $data, 'total' => $total);
    }
}